<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="任务类型名称" prop="taskName">
        <el-input
            v-model="queryParams.taskName"
            placeholder="任务类型名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>-->
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" border stripe>
      <el-table-column label="任务类型名称" align="center" prop="taskName"/>
      <el-table-column label="任务类型标识" align="center" prop="taskType"/>
      <el-table-column label="任务要求" align="center" prop="taskDemand"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >配置
          </el-button>
          <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改定时任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务类型名称" prop="taskName">
              <el-input v-model="form.taskName" placeholder="请输入任务类型名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务类型标识" prop="taskType">
              <el-input v-model="form.taskType" placeholder="请输入任务类型标识"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务要求" prop="taskDemand">
              <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.taskDemand" placeholder="请输入任务要求"/>
            </el-form-item>
          </el-col>
          <el-card v-show="form.taskType==='INSPECTION' || form.taskType==='NIGHTPATROL'" class="box-card">
            <div slot="header" class="clearfix">
              <span>参数设置</span>
            </div>
            <el-form ref="patrolSet" :model="patrolSet" :rules="patrolRules" label-width="120px">
              <el-form-item label="巡检周期" prop="cycle">
                <el-input v-model="patrolSet.cycle" placeholder="请输入巡检周期">
                  <el-select v-model="patrolSet.cycleAppend" slot="append" class="append">
                    <el-option
                        v-for="dict in timeTypeOption"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                  </el-select>
                </el-input>
              </el-form-item>
              <el-form-item label="巡检间隔" prop="space">
                <el-input v-model="patrolSet.space" placeholder="请输入巡检间隔">
                  <el-select v-model="patrolSet.spaceAppend" slot="append" class="append">
                    <el-option
                        v-for="dict in timeTypeOption"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                  </el-select>
                </el-input>
              </el-form-item>
              <el-form-item label="巡检提醒时间" prop="remind">
                <el-input v-model="patrolSet.remind" placeholder="请输入巡检提醒时间">
                  <el-select v-model="patrolSet.remindPrepend" slot="prepend" class="prepend">
                    <el-option
                        v-for="dict in remindTypeOption"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                  </el-select>
                  <el-select v-model="patrolSet.remindAppend" slot="append" class="prepend">
                    <el-option
                        v-for="dict in timeTypeOption"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                  </el-select>
                </el-input>
              </el-form-item>
            </el-form>
          </el-card>
        </el-row>
      </el-form>
      <div slot="footer" align="center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {addTaskType, delTaskType, editTaskType, listTaskType} from "@/api/taskType";

export default {
  name: "taskType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 定时任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 时间单位选择项
      timeTypeOption: [],
      // 巡检提醒选项
      remindTypeOption: [],
      // 查询参数
      queryParams: {
        taskName: undefined
      },
      //巡检参数
      patrolSet: {
        cycle: 2,
        cycleAppend: "hour",
        space: 1,
        spaceAppend: "hour",
        remind: 30,
        remindPrepend: "end",
        remindAppend: "minute"
      },
      //巡检校验
      patrolRules: {
        cycle: [
          {required: true, message: "不能为空", trigger: 'blur'}
        ],
        space: [
          {required: true, message: "不能为空", trigger: 'blur'}
        ],
        remind: [
          {required: true, message: "不能为空", trigger: 'blur'}
        ]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [{required: true, message: "任务类型名称不能为空"}],
        taskType: [{required: true, message: "任务类型标识不能为空"}],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_time_type").then(response => {
      this.timeTypeOption = response.data.data;
    });
    this.getDicts("task_type_remind").then(response => {
      this.remindTypeOption = response.data.data;
    });
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      listTaskType(this.queryParams).then(res => {
        this.taskList = res.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        taskName: undefined,
        taskType: undefined,
        taskDemand: undefined,
        taskSet: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加任务";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign({}, row)
      // this.form.id = row.id;
      // this.form.taskName = row.taskName;
      // this.form.taskType = row.taskType;
      // this.form.taskDemand = row.taskDemand;
      // this.form.taskSet = row.taskSet;
      if (this.form.taskSet) {
        this.patrolSet = JSON.parse(this.form.taskSet)
        // console.log(this.patrolSet)
      }
      this.title = "修改任务";
      this.open = true;
    },
    setTaskSet() {
      if (this.form.taskType === "INSPECTION" || this.form.taskType === "NIGHTPATROL") {
        return JSON.stringify(this.patrolSet)
      }
      return undefined
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.taskSet = this.setTaskSet()
      this.$refs["form","patrolSet"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editTaskType(this.form).then(response => {
              this.$message({
                message: "修改成功",
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          } else {
            addTaskType(this.form).then(response => {
              this.$message({
                message: "新增成功",
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobIds = row.jobId;
      this.$confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delTaskType({id: jobIds});
      }).then(() => {
        this.getList();
        this.$message({
          message: "删除成功",
          type: 'success'
        });
      }).catch(() => {
      });
    }
  }
};
</script>

<style>

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}

.prepend {
  width: 100px;
}

.append {
  width: 100px;
}
</style>
